﻿using System;
using System.IO;
using System.Collections.Generic;
using Microsoft.Xna.Framework;

namespace ThreeDContentPipeline
{
    public class DeleDImporter : ContentImporter<MapContent>
    {
        public override MapContent Import(string file, ContentImporterContext context)
        {
            MapContent map = new MapContent();
            Dictionary<int, BatchContent> batches = new Dictionary<int, BatchContent>();
            DeleDFile f = new DeleDFile(file);

            foreach (DeleDPrimitive prim in f.Primitives)
            {
                PrimitiveContent primitive = new PrimitiveContent();
                Vector3[] points = new Vector3[prim.Vertices.Count];

                foreach (DeleDVertex vert in prim.Vertices.Values)
                {
                    points[vert.ID] = vert.Position;
                }

                primitive.Name = prim.Name;
                primitive.Points.AddRange(points);

                map.Primitives.Add(primitive);

                prim.Polygons.Sort();

                foreach (DeleDPolygon poly in prim.Polygons)
                {
                    int mid = poly.MaterialID;
                    int vertexCount;
                    PolygonContent polygon = new PolygonContent();

                    if (!batches.ContainsKey(mid))
                    {
                        BatchContent batch = new BatchContent();

                        batch.Texture1 = new ExternalReference(Path.Combine(Arguments, f.Materials[mid].Layers[DeleDLayerType.Texture].Texture.File));

                        if (f.Materials[mid].Layers.ContainsKey(DeleDLayerType.Lightmap))
                        {
                            batch.Texture2 = new ExternalReference(Path.Combine(Arguments, f.Materials[mid].Layers[DeleDLayerType.Lightmap].Texture.File));
                        }
                        batches.Add(mid, batch);
                    }

                    vertexCount = batches[mid].VertexBuffer.VertexCount;

                    foreach (DeleDPolyonVertex polyVert in poly)
                    {
                        DeleDVertex vert = prim.Vertices[polyVert.VertexID];

                        batches[mid].VertexBuffer.Add(vert.Position);
                        batches[mid].VertexBuffer.Add(poly.Normal);
                        batches[mid].VertexBuffer.Add(polyVert.TextureCoordinate1);
                        batches[mid].VertexBuffer.Add(polyVert.TextureCoordinate2);

                        polygon.Indices.Add(vert.ID);
                    }
                    primitive.Polygons.Add(polygon);

                    foreach (int index in poly.Indices)
                    {
                        batches[mid].IndexBuffer.AddIndex(index + vertexCount);
                    }
                }
            }

            foreach (BatchContent batch in batches.Values)
            {
                map.Batches.Add(batch);
            }

            map.ImportTextures(context);

            return map;
        }
    }
}
